//
//  WLUserInfoModifyVController.m
//  WanLian
//
//  Created by muzhuang on 2018/10/9.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLUserInfoModifyVController.h"

@interface WLUserInfoModifyVController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textModify;

@end

@implementation WLUserInfoModifyVController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *btnDone = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    btnDone.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
    [btnDone setTitleColor:wlblueColor forState:UIControlStateNormal];
    [btnDone setTitle:@"完成" forState:UIControlStateNormal];
    [btnDone addTarget:self action:@selector(modifyDoneAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:btnDone];
    self.navigationItem.rightBarButtonItem = rightBar;
//    if (@available(iOS 11, *)) {
//        self.navigationItem.rightBarButtonItem = rightBar;
//    }
//    else
//    {
//        UIBarButtonItem *rightSpacer = [[UIBarButtonItem alloc]
//                                        initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
//                                        target:nil action:nil];
//        rightSpacer.width = -16;
//        self.navigationItem.rightBarButtonItems = @[rightSpacer,rightBar];
//    }
    
    UIView *contianView = [[UIView alloc] init];
    contianView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contianView];
    WLWkSelf
    [contianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_top).offset(WLScaleX(10));
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(@(WLScaleX(60)));
    }];
    
    _textModify = [[UITextField alloc] initWithFrame:CGRectMake(16, 0, WIDTH_SCREEN_WL-32, WLScaleX(60))];
    _textModify.returnKeyType = UIReturnKeyDone;
    _textModify.delegate = self;
    _textModify.textColor = wlBlackColor;
    _textModify.font = [WLCommonTool fontTypePingFangSCRegular:17];
    [contianView addSubview:_textModify];
    
    if (_type==1) {
        self.title = @"设置邮箱";
        _textModify.text = _userInfos.email;
    }
    else
    {
        self.title = @"更改昵称";
        _textModify.text = _userInfos.username;
        [_textModify addTarget:self action:@selector(textChangeValue:) forControlEvents:UIControlEventEditingChanged];
    }
}

- (void)modifyDoneAction
{
    NSString *textStr = _textModify.text;
    if (textStr.length<1) {
        return;
    }
    
    if (_type == 1)
    {
        if ([WLCommonTool isValidateEmail:textStr]==NO) {
            [self showHUDMessage:@"请输入正确的邮箱"];
            return;
        }
        NSDictionary *params = @{@"email":textStr};
        WLWkSelf
        [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"user/app/edit" withParamsDic:params success:^(NSDictionary *successDict) {
            WLUserInfo *userInfo = [WLUserInfo mj_objectWithKeyValues:successDict[@"object"]];
            [[WLUseManager shareUser] updateUserValue:userInfo];
//            weakSelf.userInfos = userInfo;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failure:^(NSString *errorMsg, NSInteger errorTag) {
            [weakSelf showHUDMessage:errorMsg];
        } error:^(NSError *error) {
            
        } inView:self.view];
    }
    else
    {
        NSDictionary *params = @{@"username":textStr};
        WLWkSelf
        [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"user/app/edit" withParamsDic:params success:^(NSDictionary *successDict) {
            WLUserInfo *userInfo = [WLUserInfo mj_objectWithKeyValues:successDict[@"object"]];
            [[WLUseManager shareUser] updateUserValue:userInfo];
//            weakSelf.userInfos = userInfo;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failure:^(NSString *errorMsg, NSInteger errorTag) {
            [weakSelf showHUDMessage:errorMsg];
        } error:^(NSError *error) {
            
        } inView:self.view];
    }
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textChangeValue:(UITextField *)textField
{
    if (textField.text.length>10&&textField.markedTextRange == nil)
    {
        textField.text = [textField.text substringToIndex:10];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
